home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.9 KB  |  224 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Ghost -=- a classic word-building challenge
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg menus.h"
  32. #include "msg sounds.h"
  33. #include "msg prefs.h"
  34. #include "msg environment.h"
  35. #include "ghost.h"
  36. #include "AppleEvents.h"
  37. #include "EPPC.h"
  38.  
  39. void main(void)
  40. {
  41.     MaxApplZone();    
  42.     InitGraf(&thePort);
  43.     InitFonts();
  44.     FlushEvents(everyEvent, 0);
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(0L);
  49.     InitCursor();
  50.     GetDateTime(&randSeed);
  51.     
  52.     CheckEnvironment();    
  53.     DoIntegrityCheck();    
  54.     InitSounds();
  55.     PrefsError(PreferencesInit());
  56.     InitEnvironment();    
  57.     InitMSGGraphics();
  58.     InitGame();
  59.     EventLoop();    
  60.     ShutDownEnvironment();
  61.     ExitToShell();
  62. }
  63.  
  64. void EventLoop(void)
  65. {
  66.     EventRecord        theEvent;
  67.     
  68.     while (!gDone)
  69.     {
  70.         SetCursor(&arrow);
  71.         HiliteMenu(0);
  72.         
  73.         if(gMainWindow)
  74.             SetPort(gMainWindow);
  75.         
  76.         if (WaitNextEvent(everyEvent, &theEvent, 0, 0L))
  77.             DispatchEvents(theEvent);
  78.         else
  79.             CheckForComputerPlayer();
  80.     }
  81. }
  82.  
  83. void DispatchEvents(EventRecord theEvent)
  84. {
  85.     int                i;
  86.     OSErr            isHuman;
  87.     Point            thisPoint;
  88.     
  89.     switch (theEvent.what)
  90.     {
  91.         case nullEvent:
  92.             CheckForComputerPlayer();
  93.             break;
  94.         case mouseDown:
  95.             HandleMouseDown(theEvent);
  96.             break;
  97.         case keyDown:
  98.         case autoKey:
  99.             if(theEvent.modifiers & cmdKey)
  100.             {
  101.                 AdjustMenus();
  102.                 HandleMenu(MenuKey((char)(theEvent.message & charCodeMask)));
  103.             }
  104.             else if (gMainWindow)
  105.                 GameKeyEvent((char)(theEvent.message & charCodeMask));
  106.             break;
  107.         case diskEvt:
  108.             if (HiWord(theEvent.message)!=noErr)
  109.             {
  110.                 DILoad();
  111.                 SetPt(&thisPoint, 120, 120);
  112.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  113.                 DIUnload();
  114.             }
  115.             break;
  116.         case updateEvt:
  117.             BeginUpdate((WindowPtr)theEvent.message);
  118.             
  119.             if((WindowPtr)theEvent.message == gMainWindow)
  120.                 UpdateBoard();
  121.             
  122.             for (i=0; i<NUM_HELP; i++)
  123.                 if ((WindowPtr)theEvent.message == gHelp[i])
  124.                     UpdateHelp(i);
  125.             
  126.             EndUpdate((WindowPtr)theEvent.message);
  127.             break;
  128.         case kHighLevelEvent:
  129.             if (gHasAppleEvents)
  130.                 AEProcessAppleEvent(&theEvent);
  131.             break;
  132.     }
  133. }
  134.  
  135. void HandleMouseDown(EventRecord theEvent)
  136. {
  137.     WindowPtr            theWindow;
  138.     int                    windowCode;
  139.     long                windSize;
  140.     GrafPtr                oldPort;
  141.     int                    i;
  142.     Rect                sizeRect;
  143.     
  144.     windowCode=FindWindow(theEvent.where, &theWindow);
  145.     switch (windowCode)
  146.     {
  147.         case inMenuBar:
  148.             AdjustMenus();
  149.             HandleMenu(MenuSelect(theEvent.where));
  150.             break;
  151.         case inContent:
  152.             if(FrontWindow() != theWindow)
  153.                 SelectWindow(theWindow);
  154.             else
  155.                 if(theWindow == gMainWindow)
  156.                     GameEvent();
  157.             break;
  158.         case inSysWindow:
  159.             SystemClick(&theEvent, theWindow);
  160.             break;
  161.         case inDrag:
  162.             DragWindow(theWindow, theEvent.where, &gDragRect);
  163.             if(theWindow == gMainWindow)
  164.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  165.             break;
  166.         case inGoAway:
  167.             if (TrackGoAway(theWindow, theEvent.where))
  168.             {
  169.                 for (i=0; i<NUM_HELP; i++)
  170.                     if (theWindow == gHelp[i])
  171.                         gHelp[i]=0L;
  172.                 
  173.                 if(theWindow == gMainWindow)
  174.                     CloseMainWindow();
  175.                 else
  176.                     DisposeWindow(theWindow);
  177.                 
  178.                 AdjustMenus();
  179.             }
  180.             break;
  181.         case inGrow:
  182.             sizeRect = screenBits.bounds;
  183.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  184.             
  185.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  186.             if(windSize != 0)
  187.             {
  188.                 GetPort(&oldPort);
  189.                 SetPort(theWindow);
  190.                 EraseRect(&theWindow->portRect);
  191.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  192.                 InvalRect(&theWindow->portRect);
  193.                 SetPort(oldPort);
  194.             }
  195.             
  196.             if(theWindow == gMainWindow)
  197.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  198.             
  199.             break;
  200.         case inZoomIn:
  201.         case inZoomOut:
  202.             if(TrackBox(theWindow, theEvent.where, windowCode))
  203.             {
  204.                 GetPort(&oldPort);
  205.                 SetPort(theWindow);
  206.                 ZoomWindow(theWindow, windowCode, FALSE);
  207.                 InvalRect(&theWindow->portRect);
  208.                 SetPort(oldPort);
  209.             }
  210.             
  211.             if(theWindow == gMainWindow)
  212.                 gMainWindowBounds = (*(((WindowPeek)gMainWindow)->contRgn))->rgnBBox;
  213.             
  214.             break;
  215.     }
  216. }
  217.  
  218. void ShutDownEnvironment(void)
  219. {
  220.     ShutDownGame();
  221.     ShutDownMSGGraphics();
  222.     CloseSounds();    
  223. }
  224.